. "$PSScriptRoot\fserver.ps1"

function _preview1($input1) {
    $index = 0
    foreach($item in $server_lists)
    {
        if($item[0] -eq $input1){
            #echo "$item"
            #echo $item[1]
            # echo "$($item[2]) $($item[3])"
            echo "$($item[2])"
            break;
        } else {
            $index = $index + 1
        }
    }
}


function _preview2($input1) {
    $index = 0
    foreach($item in $server_lists)
    {
        if($item[0] -eq $input1){
            #echo "$item"
            #echo $item[1]
            echo "$($item[3])"
            break;
        } else {
            $index = $index + 1
        }
    }
}

function _preview3($input1) {
    $index = 0
    foreach($item in $server_lists)
    {
        if($item[0] -eq $input1){
            #echo "$item"
            #echo $item[1]
            echo "$($item[1]) $($item[3])"
            break;
        } else {
            $index = $index + 1
        }
    }
}



function _copytext($input1) {
    $index = 0
    foreach($item in $server_lists)
    {
        if($item[0] -eq $input1){
            #echo "$item"
            #echo $item[1]
            echo "$($item[4])"
            break;
        } else {
            $index = $index + 1
        }
    }
}

function ls_get_files(){
    $fileList = Get-ChildItem 
    $filesListStr=""
    foreach($File in $fileList ) {
        $filesListStr= "$filesListStr`n$File"
    } 
    echo $filesListStr
}

function _scp() {

	# echo "[Info] this is _ssh()"
    # $select_server =$(_list | fzf)
    $select_server = $(_list | fzf --cycle  --height=81%  --preview-window=bottom:1:wrap
    )
    #echo "[Debug] fzf select server is $select_server "

    # 选择方向
    $target = $(echo  "Up`nDown" | fzf --cycle  --height=81%  --preview-window=bottom:1:wrap
    )
    # echo "[Debug] target = $target"
    # 上传文件
    # //使用方法：scp 源文件路径 账户@地址:目的路径
    # scp C:\Users\zbh\Desktop\1.txt  lucas@192.168.11.150:/home/lucas/
    # 下载文件
    # scp lucas@192.168.110.128:/home/lucas/world.txt C:\Users\zbh\Desktop\
    if($target -eq "Up"){
        # echo "[Debug] Up"
        $select_source = $( ls_get_files | fzf --cycle  --height=81%  --preview-window=bottom:1:wrap)
        echo "[Debug] select_source = $select_source"
        # 组合上传文件命令
        # echo "[Debug] select_source = $select_source"
        $cmdString1 = _preview1($select_server) 
        $cmdString2 = _preview2($select_server) 
        $cmdString_up = "$cmdString1 `.`/'$select_source'  $cmdString2`:~"
        # echo "[Debug] cmdString = $cmdString_up"
        echo  "copy $select_server passwd  ..."
        Set-Clipboard -Value $(_copytext "$select_server")
        echo "Exp: $cmdString_up"
        Invoke-Expression $cmdString_up
    }
    if($target -eq "Down"){
        # echo "[Debug] Down"
        # 组装命令执行进入远程主机查看文件列表
        $cmdString1 = _preview3($select_server) 
        $cmdString_remote_pwd = "$cmdString1  ls"
        # echo "[Debug] cmdString_remote_pwd = $cmdString_remote_pwd"
        echo  "copy $select_server passwd  ..."
        Set-Clipboard -Value $(_copytext "$select_server")
        echo "Exp: $cmdString_remote_pwd"
        $remote_ls =$(Invoke-Expression $cmdString_remote_pwd)
        #echo "[Debug] remote_ls = $remote_ls"

        $select_source = $( echo $remote_ls | fzf --cycle  --height=81%  --preview-window=bottom:1:wrap)
        echo "[Debug] select_source = $select_source"

        $cmdString1 = _preview1($select_server) 
        $cmdString2 = _preview2($select_server) 
        $cmdString_down = "$cmdString1  $cmdString2`:`~`/$select_source `.`/"
        echo "[Debug] cmdString_down = $cmdString_down"

        echo  "copy $select_server passwd  ..."
        Set-Clipboard -Value $(_copytext "$select_server")
        # 打印要执行的命令
         echo "Exp: $cmdString_down"
        Invoke-Expression $cmdString_down
    }
}



switch ($args[0])
{
	"" { _scp }
	preview { _preview($args[1])}
	copytext {_copytext "$args[1]"}
	cmdstr { _cmdstr "$args[1]"}
	default { "Tt is default"}
}